package com.fqcar.fqdf.core.http;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.Response.Listener;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyLog;
import com.fqdf.framework.android.util.CheckLogicUtil;

import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MultipartTOptRequest<T> extends ResponseTOptRequest<T> {

    private MultipartEntity mEntity;
    private List<File> fileList;
    private static RetryPolicy mRetryPolicy = new DefaultRetryPolicy(3 * 60 * 1000, 0, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
    private static final String FILE_NAME = "file";


    public MultipartTOptRequest(String url, Map<String, String> params, Map<String, String> headers
            , File fileItem, Listener<T> listener, Response.ErrorListener errorListener) {

        super(url, params, headers, listener, errorListener);

        if (fileItem == null) {
            throw new RuntimeException("files is null");
        }
        fileList = new ArrayList<>();
        fileList.add(fileItem);
        setRetryPolicy(mRetryPolicy);
    }


    public MultipartTOptRequest(String url, Map<String, String> params, Map<String, String> headers, List<File> fileList
            , Listener<T> listener, Response.ErrorListener errorListener) {
        super(url, params, headers, listener, errorListener);

        if (CheckLogicUtil.isEmpty(fileList)) {
            throw new RuntimeException("files is null");
        }
        this.fileList = fileList;
        setRetryPolicy(mRetryPolicy);
    }

    @Override
    public String getBodyContentType() {
        return mEntity.getContentType().getValue();
    }

    @Override
    public byte[] getBody() throws AuthFailureError {
        mEntity = new MultipartEntity();
        ByteArrayOutputStream bos = null;
        byte[] bytes = null;
        try {
            for (File item : fileList) {
                if (item == null || !item.exists()) {
                    throw new RuntimeException("the value of files at index is null or file not exists");
                }
                mEntity.addPart(FILE_NAME, new FileBody(item));
            }

            for (Map.Entry<String, String> entry : getParams().entrySet()) {
                mEntity.addPart(entry.getKey(), new StringBody(entry.getValue(), Charset.defaultCharset()));
            }
            bos = new ByteArrayOutputStream();
            mEntity.writeTo(bos);
            bytes = bos.toByteArray();
            bos.flush();
        } catch (IOException e) {
            VolleyLog.e("IOException writing to ByteArrayOutputStream");
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    VolleyLog.e("IOException close io error");
                }
            }
        }
        return bytes;
    }
}